package com.beamfield.controller.manage.sys;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.SystemDictionaryData;
import com.beamfield.modelVo.SystemDictionaryDataVo;
import com.beamfield.modelVo.TreeVo;
import com.beamfield.service.manage.sys.DictonaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cms/sys")
public class DictonaryController {

	@Resource
	private DictonaryService dictonaryService;

	@Value("${session.beam}")
	private String beam;

	@Value("${session.user}")
	private String user;

	/**
	 * 跳转数据字典页面
	 * @return
	 */
	@RequestMapping("/dictonary")
	public ModelAndView dictonary(HttpSession session){
		ModelAndView mv = new ModelAndView();
		 return mv;
	}


	/**
	 * 数据字典页面
	 * @return
	 */
	@RequestMapping("/dictonaryData")
	@ResponseBody
	public PageResult dictonaryData(Integer offset, Integer limit,HttpSession session, String dic_name, String dicCode){
		long beam_id = (long) session.getAttribute(beam);
		return dictonaryService.getDictonaryList(offset, limit,beam_id, dic_name, dicCode);
	}

	/**
	 * 跳转数据字典页面
	 * @return
	 */
	@RequestMapping("/dirList")
	@ResponseBody
	public ApiResult dirList(HttpServletRequest request){
		long beam_id = (long) request.getSession().getAttribute(beam);
		return dictonaryService.getDirList(beam_id);
	}

	@RequestMapping("/getMaxDicCode")
	@ResponseBody
	public ApiResult getMaxDicCode(Long sys_dictionary_id,HttpSession session){
		long beam_id = (long) session.getAttribute(beam);
		String code = dictonaryService.getMaxDicCode(sys_dictionary_id, beam_id);
		return new ApiResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),code);
	}

	@RequestMapping("/saveDic")
	@ResponseBody
	public ApiResult saveDic(SystemDictionaryData systemDictionaryData,HttpSession session){
		long userId = (long) session.getAttribute(this.user);
	    long beamId = (long) session.getAttribute(beam);
	    systemDictionaryData.setOperator(userId);
	    systemDictionaryData.setBeam_id(beamId);
	    systemDictionaryData.setUpdater(userId);
		return dictonaryService.saveDic(systemDictionaryData);
	}

	@RequestMapping("/delDic")
	@ResponseBody
	public ApiResult delDic(String id,HttpSession session){
		long userId = (long) session.getAttribute(this.user);
		if(StringUtils.isEmpty(id)){
			return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
		}

		return dictonaryService.delDic(id, userId);
	}

	@RequestMapping("/selData")
	@ResponseBody
	public ApiResult selData(String code,HttpSession session){
		long beam_id = (long) session.getAttribute(beam);
		ApiResult apiResult = new ApiResult();
        List<SystemDictionaryDataVo> dicByCode = dictonaryService.getDicByCode(code,beam_id);
        apiResult.setResult(dicByCode);
        return apiResult;
    }

	@RequestMapping("/treeData")
	@ResponseBody
	public ApiResult treeData(HttpSession session, @RequestParam("code[]") String[] codes){
		long beam_id = (long) session.getAttribute(beam);
		ApiResult apiResult = new ApiResult();
		List<SystemDictionaryData> dicByCode = dictonaryService.getDicByCodes(codes,beam_id);
		List<TreeVo> treeVoList = new ArrayList<>();
		for(SystemDictionaryData systemDictionaryData : dicByCode){
			TreeVo treeVo = new TreeVo();
			treeVo.setText(systemDictionaryData.getName());
			treeVo.setValue(systemDictionaryData.getCode());
			treeVoList.add(treeVo);
		}
		apiResult.setResult(treeVoList);
		return apiResult;
	}

	@RequestMapping("/saveDeviceSource")
	@ResponseBody
	public ApiResult saveDeviceSource(Long id, String name,HttpSession session){
		Long userId = (long) session.getAttribute(user);
		Long beamId = (long) session.getAttribute(beam);
		return dictonaryService.saveDeviceSource(id,name,userId,beamId);
	}
}
